# -*- coding: utf-8 -*-
"""
Created on Tue Jun  5 21:16:41 2018

@author: Administrator
"""

from agilepipe.spine import AgilePipe


#%%
#a example for  manipulate_methods: 'slice_timing_ST':{'tpattern':['seqplus','altplus']
#%%t
#he code below is to coregister PD to CBF

modality = '_T1'   #the image modality in the original_dirpath   '_T1' '_BOLD' '_CBF'  '_PET'
manipulate_methods = { 'Bet_fast_T1_BF':{}
                      }
#dirpath 也是一个dict，其keys与manipulate_methods完全相同，但元素是对应处理方法所生成的文件夹的名字。
dirpath_dict = {'original_dirpath':[],
                     #用来存放其实的文件。
                     'Bet_fast_T1_BF':[]
                    }

#agile_pipe()用ccs处理BOLD的测试代码
original_dirpath = r'/media/root/Elements3/XuanWu_gz_agilepipe/T1'
#the first element of dirpath_list is very important which is used as reference image when coregister CBF to T1
dirpath_list = ['',''] #即使没有用也要赋值，否则会报错
dirpath_dict = dirpath_dict
manipulate_methods = manipulate_methods
parallel_core = 0.7
t1_result = AgilePipe(modality,original_dirpath,dirpath_list,dirpath_dict,manipulate_methods,parallel_core)
t1_result_dirpath_list = t1_result.dirpath_dict['Bet_fast_T1_BF']


#%%
#the code below is used to process the T1 image
modality = '_CBF'   #the image modality in the original_dirpath   '_T1' '_BOLD' '_CBF'  '_PET'
manipulate_methods = { 'Bet_flirt_applywarp_pet_BFAP':{},
                      'Resample_RS':{},
                      'Smooth_spatial__SMSP':{'fwhm':['6']}
                      }
#dirpath 也是一个dict，其keys与manipulate_methods完全相同，但元素是对应处理方法所生成的文件夹的名字。
dirpath_dict = {'original_dirpath':[],
                 #用来存放其实的文件。
                 'Bet_flirt_applywarp_pet_BFAP':[],
                 'Resample_RS':[],
                 'Smooth_spatial__SMSP':[]
                    }

#agile_pipe()用ccs处理BOLD的测试代码
original_dirpath = r'/media/root/Elements3/XuanWu_gz_agilepipe/CBF_clearbg'
#the first element of dirpath_list is very important which is used as reference image when coregister CBF to T1
dirpath_list = [t1_result_dirpath_list[0],'/root/Documents/BrainMask_05_61x73x61.nii.gz',''] #即使没有用也要赋值，否则会报错
dirpath_dict = dirpath_dict
manipulate_methods = manipulate_methods
parallel_core = 0.7
pet_result = AgilePipe(modality,original_dirpath,dirpath_list,dirpath_dict,manipulate_methods,parallel_core)
pet_result_dirpath_list = pet_result.dirpath_dict['Resample_RS']
pet_result_dirpath_list_1 = pet_result.dirpath_dict['Smooth_spatial__SMSP']

#%%
#the code below is used to process the CBF image
modality = '_CBF'     #'_T1' '_BOLD' '_CBF'  '_PET'
manipulate_methods = { 'Bet_flirt_applywarp_cbf_BFAC':{},
                      'Resample_RS':{},
                      'Smooth_spatial__SMSP':{'fwhm':['6']}
                      }
#dirpath 也是一个dict，其keys与manipulate_methods完全相同，但元素是对应处理方法所生成的文件夹的名字。
dirpath_dict = {'original_dirpath':[],
                     #用来存放其实的文件。
                     'Bet_flirt_applywarp_cbf_BFAC':[],
                     'Resample_RS':[],
                     'Smooth_spatial__SMSP':[]
                    }

#agile_pipe()用ccs处理BOLD的测试代码
original_dirpath = r'/media/root/Elements3/XuanWu_gz_agilepipe/CBF_clearbg'
#the first element of dirpath_list is very important which is used as deformation field 
#the second element of dirpath_list can be used in the reslice processing as the "Image Defining Space" filepath
dirpath_list = [t1_result_dirpath_list[0],'/root/Documents/BrainMask_05_61x73x61.nii','/media/root/Elements3/XuanWu_gz_agilepipe/PD'] #即使没有用也要赋值，否则会报错
dirpath_dict = dirpath_dict
manipulate_methods = manipulate_methods
parallel_core = 0.7
cbf_result = AgilePipe(modality,original_dirpath,dirpath_list,dirpath_dict,manipulate_methods,parallel_core)
cbf_result_dirpath_list = cbf_result.dirpath_dict['Resample_RS']
cbf_result_dirpath_list_1 = cbf_result.dirpath_dict['Smooth_spatial__SMSP']

#%%

#make PET gourpmask before Z_standardize

#from agilepipe.process import  Create_groupmask
#
#source_dirpath = pet_result_dirpath_list[0]
#mnimask_filepath = r'/root/Documents/BrainMask_05_61x73x61.nii'
#pet_creat_groupmask = Create_groupmask([source_dirpath,mnimask_filepath])
#pet_gourpmask_filepath = pet_creat_groupmask.groupmask_filepath

#%%
#make CBF groupmask before Z_standardize

from agilepipe.process import  Create_groupmask

source_dirpath = pet_result_dirpath_list_1[0]
mnimask_filepath = r'/root/Documents/BrainMask_05_61x73x61.nii'
cbf_creat_groupmask = Create_groupmask([source_dirpath,mnimask_filepath])
cbf_gourpmask_filepath = cbf_creat_groupmask.groupmask_filepath


#%%
#perform Z_standardiz to PET

#from agilepipe.process import Z_standardize #,Mean_division
#
#previous_dirpath = pet_result_dirpath_list_1[0]
#mask_filepath = pet_gourpmask_filepath.replace
#present_dirpath = pet_result_dirpath_list_1[0] + r'_z'
#
#Z_standardize([previous_dirpath,mask_filepath,present_dirpath])

##%% mean division
#
#previous_dirpath = r'/media/root/Elements3/standardize_test/CBF_clear_background_SN_SR_SSM-6'
#mask_filepath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/CBF_processing/make_mask_before_z_standardize/sws_CBF_26_intersect_mask.nii'
#present_dirpath = r'/media/root/Elements3/standardize_test/CBF_clear_background_SN_SR_SSM-6_md'
#
#Mean_division([previous_dirpath,mask_filepath,present_dirpath])

#%%

#perform Z_standardiz to CBF

from agilepipe.process import Z_standardize #,Mean_division

previous_dirpath = cbf_result_dirpath_list_1[0]
mask_filepath = cbf_gourpmask_filepath.replace
present_dirpath = cbf_result_dirpath_list_1[0] + r'_z'

Z_standardize([previous_dirpath,mask_filepath,present_dirpath])

##%% mean division
#
#previous_dirpath = r'/media/root/Elements3/standardize_test/CBF_clear_background_SN_SR_SSM-6'
#mask_filepath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/CBF_processing/make_mask_before_z_standardize/sws_CBF_26_intersect_mask.nii'
#present_dirpath = r'/media/root/Elements3/standardize_test/CBF_clear_background_SN_SR_SSM-6_md'
#
#Mean_division([previous_dirpath,mask_filepath,present_dirpath])


#%%begin statistic analysis
#%%
#%%
#%%
#%%
#
#from agilepipe.statics  import Multi_across_subject_correlate , Multi_across_voxel_correlate,Multi_across_voxel_groupmean_correlate
#
## the code below is the result filepath of kinds of modality
#zpet_dirpath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/PET_processing/PET_normalised_resliced_smoothed_Zstandardized'
#zcbf_dirpath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/CBF_processing/CBF_clear_background_SN_SR_SSM-6_z'
#zfalff_dirpath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/BOLD_processing/fALFF_z'
#zalff_dirpath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/BOLD_processing/ALFF_Z'
#zreho_dirpath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620/BOLD_processing/ReHo_z'
##the code below is the groupmask filepath of kinds of modality
#cbf_groupmask_filepath = r'/media/root/Elements3/XuanWu_dpabi_spm/CBF_processing_ag/CBF_clear_background_SN_SR_SSM-6_groupmask/groupmask.nii'
#bold_groupmask_filepath = r''
#pet_groupmask_filepath = r''
#mni_brainmask_filepath = r'/root/Documents/BrainMask_05_61x73x61.nii'
#multi_modalitymask_filepath = r''
#output_dirpath = r'/media/root/Elements3/XuanWu_z_sm666_pearson_0620'
##the code below is the used as the excel title.
#column_name_list = ['coeff','pvalue','modalities']  #must set value ,even set column_name_list =[]
#
#
#grouppath_dict = {'PET/CBF':[zpet_dirpath,zcbf_dirpath,cbf_groupmask_filepath,output_dirpath],\
#                 'PET/fALFF':[zpet_dirpath,zfalff_dirpath,mni_brainmask_filepath,output_dirpath],\
#                 'PET/ALFF':[zpet_dirpath,zalff_dirpath,mni_brainmask_filepath,output_dirpath],\
#                 'PET/ReHo':[zpet_dirpath,zreho_dirpath,mni_brainmask_filepath,output_dirpath],\
#                 'CBF/fALFF':[zcbf_dirpath,zfalff_dirpath,cbf_groupmask_filepath,output_dirpath],\
#                 'CBF/ALFF':[zcbf_dirpath,zalff_dirpath,cbf_groupmask_filepath,output_dirpath],\
#                 'CBF/ReHo':[zcbf_dirpath,zreho_dirpath,cbf_groupmask_filepath,output_dirpath] }
#print('grouppath_dict:',grouppath_dict)
#
#Multi_across_subject_correlate([grouppath_dict,column_name_list])
#Multi_across_voxel_correlate([grouppath_dict,column_name_list])
#Multi_across_voxel_groupmean_correlate([grouppath_dict,column_name_list])